<template>
  <div class="app-container">
    <h1>班级信息列表</h1>
    <el-divider/>

    <el-form :inline="true" :model="classQuery" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input v-model="classQuery.name" placeholder="请输入班级名称"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getClassList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-divider/>

    <el-button-group>
      <el-button type="success" size="small" @click="handleAddClass">
        <svg-icon icon-class="add"/>
        添加班级
      </el-button>
      <el-popconfirm
        title="确定删除选中班级么？（该操作会删除这些班级内所有学生及其答辩）"
        icon-color="red"
        icon="el-icon-info"
        @confirm="submitDeleteClasses"
      >
        <el-button slot="reference" :disabled="isDisabled" type="danger" size="small">
          <svg-icon icon-class="remove"/>
          删除选中班级
        </el-button>
      </el-popconfirm>
    </el-button-group>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      stripe
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column
        prop="name"
        fixed="left"
        label="名称"/>
      <el-table-column
        prop="count"
        label="人数"/>
      <el-table-column fixed="right" label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="getClassInfo(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <div class="block">
      <el-pagination
        :current-page="page"
        :page-sizes="[5, 10, 15, 30, 50, 100]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="changSize"
        @current-change="changePage"/>
    </div>

    <el-dialog :close-on-click-modal="false" :visible.sync="classInfoFlag" title="详细信息">
      <el-table :data="classInfo">
        <el-table-column property="name" label="姓名"/>
        <el-table-column property="noId" label="学号"/>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-popconfirm
          title="确定删除此班级么？（该操作会删除此班级内所有学生及其答辩）"
          icon-color="red"
          icon="el-icon-info"
          @confirm="submitDelete"
        >
          <el-button slot="reference" type="danger">删除班级</el-button>
        </el-popconfirm>
        <el-button type="info" @click="classInfoFlag = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :visible.sync="addClassFlag" title="添加班级">
      <el-form ref="addClass" :model="addClass" label-width="100px">
        <el-form-item
          :rules="[
            { required: true, message: '请输入班级名称', trigger: 'blur' }
          ]"
          prop="name"
          label="名称">
          <el-input v-model="addClass.name"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAddClass('addClass')">添加</el-button>
        <el-button type="info" @click="addClassFlag = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import classApi from '@/api/admin/class'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      page: 1,
      total: 0,
      pageSize: 10,
      LabelWidth: '120px',
      isDisabled: true,
      multipleSelection: [],

      classQuery: {
        page: 1,
        limit: 10,
        name: null
      },

      classInfoFlag: false,
      classInfoId: null,
      classInfo: null,

      addClassFlag: false,
      addClass: {
        count: 0,
        name: ''
      }
    }
  },
  created() {
    this.getClassList()
  },
  methods: {
    getClassList() {
      this.listLoading = true
      this.classQuery.page = this.page
      this.classQuery.limit = this.pageSize
      classApi.queryClasses(this.classQuery).then(res => {
        this.listLoading = false
        this.total = Number.parseInt(res.result.counts)
        this.list = res.result.items
      }).catch(error => {
        console.log(error)
      })
    },
    changSize(pageSize) {
      this.pageSize = pageSize
      this.getClassList()
    },
    changePage(page) {
      this.page = page
      this.getClassList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(clazz => clazz.id)
      this.isDisabled = this.multipleSelection.length === 0
    },
    handleAddClass() {
      this.addClassFlag = true
      this.addClass = {
        count: 0,
        name: ''
      }
    },
    submitDeleteClasses() {
      classApi.deleteClasses(this.multipleSelection).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.multipleSelection = []
        this.getClassList()
      }).catch(error => {
        console.log(error)
      })
    },
    getClassInfo(id) {
      classApi.queryClassStudents(id).then(res => {
        this.classInfoId = id
        this.classInfo = res.result.items
        this.classInfoFlag = true
      }).catch(error => {
        console.log(error)
      })
    },
    submitDelete() {
      classApi.deleteClass(this.classInfoId).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.classInfoFlag = false
        this.multipleSelection = []
        this.getClassList()
      }).catch(error => {
        console.log(error)
      })
    },
    submitAddClass(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          classApi.addClass(this.addClass).then(res => {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
            this.addClassFlag = false
            this.getClassList()
          }).catch(error => {
            console.log(error)
          })
        } else {
          this.$message({
            message: '请按规则完善信息',
            type: 'warning'
          })
          return false
        }
      })
    }
  }
}
</script>
